package my.controller;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import my.common.Util;
import my.interceptor.Login;
import my.model.Post;
import my.service.PostService;

/**
 * Created by Administrator on 2017/6/14.
 */
public class PostController extends Controller {
    private static PostService postService = new PostService();

    @Before(Login.class)
    public void save() {
        Post post = getModel(Post.class, "", true);
        boolean success = postService.save(post);
        String message = success ? "文章添加成功" : "文章添加失败";
        renderJson(Util.ajaxJson(success, message));
    }

    public void index() {
        int id = getParaToInt(0, -1);
        if (id == -1) {
            renderError(404);
            return;
        }
        Record post = postService.getPostById(id);
        setAttr("post", post);
        renderFreeMarker("view.ftl");

    }


}
